package com.han.jdbc.mybatis._03;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import com.han.jdbc.mybatis._03.mappers.EmpMapper1;
import com.han.jdbc.mybatis.bean.Emp;

/**
* @author hanpeng
* @version 创建时间：2020年7月18日 下午8:23:12
* @ClassName 类名称
* @Description 类描述
*/
public class MapperTest {
	public static void main(String[] args) {
		
	}
	@Test
	public void testGetEmpByEId() {
		String resource="03mybatis-config.xml";
		SqlSession sqlSession=null;
		try {
			InputStream is = Resources.getResourceAsStream(resource);
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
			sqlSession = sqlSessionFactory.openSession();
			EmpMapper1 mapper = sqlSession.getMapper(EmpMapper1.class);
			Emp emp = mapper.getEmpByEId(1);
			System.out.println(emp);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			sqlSession.close();
		}
	}
	
	@SuppressWarnings("null")
	@Test
	public void testAdd() {
		String resource="03mybatis-config.xml";
		SqlSession sqlSession=null;
		try {
			InputStream is = Resources.getResourceAsStream(resource);
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
			sqlSession = sqlSessionFactory.openSession();
			EmpMapper1 mapper = sqlSession.getMapper(EmpMapper1.class);
			
			Emp emp = new Emp();
			emp.setAge(112);
			emp.seteName("xijinp");
			emp.setSex("女");
			boolean b = mapper.addEmp(emp);
			System.out.println(emp+"===>"+b);
			sqlSession.commit();
		} catch (IOException e) {
			sqlSession.rollback();
			e.printStackTrace();
		} finally {
			sqlSession.close();
		}
	}
	@Test
	public void testGetEmpByEIdAndName() {
		String resource="03mybatis-config.xml";
		SqlSession sqlSession=null;
		try {
			InputStream is = Resources.getResourceAsStream(resource);
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
			sqlSession = sqlSessionFactory.openSession();
			EmpMapper1 mapper = sqlSession.getMapper(EmpMapper1.class);
			
			Emp emp = mapper.getEmpByEIdAndName(1, "xijinpsa");
			System.out.println(emp);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			sqlSession.close();
		}
	}
	@Test
	public void testGetEmpByMap() {
		String resource="03mybatis-config.xml";
		SqlSession sqlSession=null;
		try {
			InputStream is = Resources.getResourceAsStream(resource);
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
			sqlSession = sqlSessionFactory.openSession();
			EmpMapper1 mapper = sqlSession.getMapper(EmpMapper1.class);
			Map<String, Object> map = new HashMap<>();
			map.put("eId", 1);
			map.put("eName", "张三");
			Emp emp = mapper.getEmpByMap(map);
			System.out.println(emp);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			sqlSession.close();
		}
	}
	
	@Test
	public void testGetEmpByEIdReturnMap() {
		String resource="03mybatis-config.xml";
		SqlSession sqlSession=null;
		try {
			InputStream is = Resources.getResourceAsStream(resource);
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
			sqlSession = sqlSessionFactory.openSession();
			EmpMapper1 mapper = sqlSession.getMapper(EmpMapper1.class);
			Map<Integer, Emp> map = mapper.getEmpByEIdReturnMap(1);
			System.out.println(map);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			sqlSession.close();
		}
	}
	@Test
	public void testFirstLevel() {
		String resource="03mybatis-config.xml";
		SqlSession sqlSession=null;
		try {
			InputStream is = Resources.getResourceAsStream(resource);
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
			sqlSession = sqlSessionFactory.openSession();
			EmpMapper1 mapper = sqlSession.getMapper(EmpMapper1.class);
			Emp emp = mapper.getEmpByEId(1);
			System.out.println(emp);
			Emp emp1 = mapper.getEmpByEId(1);
			System.out.println(emp1);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			sqlSession.close();
		}
	}
	@Test
	public void testSecondLevel() {
		String resource="03mybatis-config-cache.xml";
		SqlSession sqlSession=null;
		SqlSession sqlSession1=null;
		try {
			InputStream is = Resources.getResourceAsStream(resource);
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
			sqlSession = sqlSessionFactory.openSession();
			sqlSession1 = sqlSessionFactory.openSession();
			EmpMapper1 mapper = sqlSession.getMapper(EmpMapper1.class);
			EmpMapper1 mapper2 = sqlSession1.getMapper(EmpMapper1.class);
			Emp emp = mapper.getEmpByEId(1);
			System.out.println(emp);
			sqlSession.close();
			System.out.println("+++++++++++");
			Emp emp1 = mapper2.getEmpByEId(1);
			Emp emp2 = mapper2.getEmpByEId(1);
			System.out.println(emp1);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			sqlSession.close();
		}
	}
}
